package com.base.cn.platform.os.service.user.gradeconfig;


import com.base.cn.platform.os.common.mybatis.BaseBiz;
import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.dao.user.gradeconfig.CusUserGradeConfigDao;
import com.base.cn.platform.os.entity.user.gradeconfig.CusUserGradeConfig;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 等级Biz
 * Created by ll on 2017/6/21 0021.
 */
@Service
public class CusUserGradeConfigBiz extends BaseBiz<CusUserGradeConfig,CusUserGradeConfigDao> {

    /**
     * 取下一级，用户积分等级
     * @param level  等级名称
     * @return
     */
    public String getLevelNext(String level){
        String nextLevel = "LV";
        if(ObjectUtils.isNotEmpty(level)){
           String[] up = level.split(nextLevel);
            if(ObjectUtils.isNotEmpty(up)){
                Integer i  = Integer.valueOf(up[1]);
                nextLevel = nextLevel+(i+1);
            }
        }
        return nextLevel;
    }

    /**
     * 公共搜索列表
     * @param page  分页条件
     * @return
     */
    public PageInfo<CusUserGradeConfig> queryUserGradeConfigPage(Pagination page){
        String whereSql =" 1=1 ";
        return this.findPage(whereSql,page,new ArrayList<>());
    }

    /**
     * 获取sql
     * @param cusUserGradeConfig
     * @return
     */
     public String  whereSql(CusUserGradeConfig cusUserGradeConfig){
         String  where = " 1 = 1 ";
         if (ObjectUtils.isNotEmpty(cusUserGradeConfig)) {
            if(ObjectUtils.isNotEmpty(cusUserGradeConfig.getGradeName())){
                where += " and gradeName like '%"+cusUserGradeConfig.getGradeName().trim()+"%'";
            }
            if(ObjectUtils.isNotEmpty(cusUserGradeConfig.getGradeTitle())){
                where += " and gradeTitle like '%"+cusUserGradeConfig.getGradeTitle().trim()+"%'";
            }
            if (ObjectUtils.isNotEmpty(cusUserGradeConfig.getExperience())){
                where += " and experience >="+cusUserGradeConfig.getExperience();
            }
            /*if (ObjectUtils.isNotEmpty(cusUserGradeConfig.getExperience())&&cusUserGradeConfig.getStatus()>0){
                where += " and status ="+cusUserGradeConfig.getStatus();
            }*/
         }
         return where;
     }

    /**
     * 公共搜索列表
     * @return
     */
    public List<CusUserGradeConfig> queryUserGradeConfigList(){
        String whereSql =" 1=1 ";
        return this.find(whereSql,null,new ArrayList<>());
    }


    /**
     * 搜索 开始分数和 结束分数
     * @param startLevel 开始等级
     * @param endLevel  结束等级
     * @return
     */
    public Map<String,Object>  getStartAndEndExp(String startLevel,String endLevel){
        String whereLevel = " gradeName ='"+ startLevel+ "' or gradeName ='"+endLevel+"'";
        List<CusUserGradeConfig> userGradeConfigs =this.find(whereLevel,null,new ArrayList<>());
        Long  startExp =0l;
        Long  ednExp = 0l;
        if(ObjectUtils.isNotEmpty(userGradeConfigs)){
            if(userGradeConfigs.size()==2){
                for(CusUserGradeConfig userGradeConfig : userGradeConfigs){
                    if(userGradeConfig.getGradeName().equals(startLevel)){
                        startExp = userGradeConfig.getExperience();
                    }
                    if(userGradeConfig.getGradeName().equals(endLevel)){
                        ednExp=userGradeConfig.getExperience();
                    }
                }
            }else if(userGradeConfigs.size()==1){
                startExp = userGradeConfigs.get(0).getExperience();
            }
        }
        Map<String,Object>  expMap = new HashMap<>();
        expMap.put("startExp",startExp);
        expMap.put("endExp",ednExp);
        expMap.put("userGradeConfigs",userGradeConfigs);
        return expMap;
    }

    /**
     * 添加和修改方法
     * @param userGradeConfig
     * @return
     */
    public CusUserGradeConfig  addUpdateGradeConfig(CusUserGradeConfig userGradeConfig){
        if(DataUtil.idIsNotNull(userGradeConfig.getId())){
            CusUserGradeConfig _userGradeConfig = this.findById(userGradeConfig.getId());
            _userGradeConfig.setExperience(userGradeConfig.getExperience());
            _userGradeConfig.setGradeTitle(userGradeConfig.getGradeTitle());
            this.updateById(_userGradeConfig,null);
            return _userGradeConfig;
        }else {
            this.save(userGradeConfig);
            return userGradeConfig;
        }
    }

    /**
     * 获取用户等级
     * @param exp 用户的经验
     * @return
     */
    public int queryUserLevel(Long exp){
        if(exp!=null){
            List<CusUserGradeConfig> userGradeConfigList = this.find(" experience <= " + exp + " order by experience desc",null,new ArrayList<>());
            if(ObjectUtils.isNotEmpty(userGradeConfigList)){
                CusUserGradeConfig userGradeConfig = userGradeConfigList.get(0);
                String level = userGradeConfig.getGradeName();
                level = level.replaceAll("LV","");
                int lv = Integer.parseInt(level);
                return lv;
            }
        }
        return 1;
    }
}
